package cn.edu.jdbc;

import cn.edu.jdbc.util.JdbcUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 登录
 */
public class JdbcDemo10 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            st = conn.createStatement();

            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名：");
            String un = sc.nextLine();
            System.out.println("请输入密码：");
            String pwd = sc.nextLine();

            String sql = "select * from mydb01 where username='"+un + "' and password = '"+ pwd +"'";
            rs = st.executeQuery(sql);
            if (rs.next()){
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String nickname = rs.getString("nickname");
                int age = rs.getInt("age");
                System.out.println(id+"," + username + "," + password + "," + nickname + "," + age);
                System.out.println(nickname + "登录成功~~~");

            } else {
                System.out.println("用户名或者密码错误~~");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(rs, st, conn);
        }
    }
}
